package operation;

import book.bookList;

import java.util.Scanner;

/**
 * @Author @fiber-cloud
 * @Date 2025/7/13 15:32
 * 删除图书
 */
public class DelOperation implements IOperation{

    @Override
    public void work(bookList booklist) {
        System.out.println("删除图书");

        //输入要删除书的名字
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的图书的书名");
        String name = sc.nextLine();

        //遍历这本书是否存在
        for (int i = 0; i < booklist.getCount(); i++) {
            if (name.equals(booklist.getBooks(i).getName())){
                //如果存在这本书，那么进行删除操作，将后面的书往前移动
                int index = i;
                for (int j = index; j < booklist.getCount()-1; j++) {
                    booklist.setBooks(j, booklist.getBooks(j+1));
                }

                //删掉目标书籍后，整体书架数量减一
                booklist.setCount(booklist.getCount()-1);
                System.out.println("《"+name+"》"+"删除成功！");
                return;
            }
        }
        System.out.println("《"+ name + "》" + "没有要删的这本书");
    }
}
